package com.hyk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.ann.RequestType;
import com.hyk.domain.SysUser;
import com.hyk.dto.AdminUserDto;
import com.hyk.dto.R;
import com.hyk.enums.RequestTypeEnum;
import com.hyk.fegin.AdminUserFegin;
import com.hyk.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户控制器")
@RequestMapping("/users")
public class SysUserController implements AdminUserFegin {

    @Resource
    private SysUserService sysUserService;

    @RequestType(RequestTypeEnum.QUERY)
    @GetMapping
    @ApiOperation(value = "查询员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile",value = "手机号"),
            @ApiImplicitParam(name = "fullname",value = "姓名"),
            @ApiImplicitParam(name = "current",value = "当前页数"),
            @ApiImplicitParam(name = "size",value = "每页数量")
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> list(@ApiIgnore Page<SysUser> page,
                                 @RequestParam(value = "mobile",required = false) String mobile,
                                 @RequestParam(value = "fullname",required = false) String fullname){
        Page<SysUser> list = sysUserService.findList(page,mobile,fullname);
        return R.ok(list);
    }

    @RequestType(RequestTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile",value = "手机号"),
            @ApiImplicitParam(name = "fullname",value = "姓名"),
            @ApiImplicitParam(name = "email",value = "邮箱"),
            @ApiImplicitParam(name = "password",value = "密码"),
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "role_strings",value = "角色id"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R insert(@RequestBody SysUser sysUser){
        sysUserService.saveUser(sysUser);
        return R.ok();
    }

    @RequestType(value = RequestTypeEnum.UPDATE)
    @PatchMapping
    @ApiOperation(value = "编辑员工")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "mobile",value = "手机号"),
                    @ApiImplicitParam(name = "fullname",value = "姓名"),
                    @ApiImplicitParam(name = "email",value = "邮箱"),
                    @ApiImplicitParam(name = "id",value = "用户id"),
                    @ApiImplicitParam(name = "password",value = "密码"),
                    @ApiImplicitParam(name = "username",value = "用户名"),
                    @ApiImplicitParam(name = "role_strings",value = "角色id"),
                    @ApiImplicitParam(name = "status",value = "状态"),
            }
    )
    @PreAuthorize("hasAuthority('sys_user_update')")
    public R update(@RequestBody SysUser sysUser){
        sysUserService.updateUser(sysUser);
        return R.ok();
    }

    @RequestType(value = RequestTypeEnum.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除员工")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "ids",value = "用户id数组"),
            }
    )
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R delete(@RequestBody Long[] ids){
        sysUserService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @Override
    @GetMapping("/getAdminUser")
    public Map<Long, AdminUserDto> getAdminUser(List<Long> ids) {
        return sysUserService.getAdminUser(ids);
    }
}
